﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;

public class ConsoleConmand
{
    public string id;
    public Action<List<string>> callback;
}

public class CommandInput : ConsoleComponent
{
    private Dictionary<string, ConsoleConmand> commandDic; // The name to command
    private string commandInput;

    public CommandInput()
    {
        commandInput = "";
        commandDic = new Dictionary<string, ConsoleConmand>();
    }

    public override void OnDrawing()
    {
        // Set textField control name to focus it 
        string controlName = "commandInput";
        GUI.SetNextControlName(controlName);

        // Draw command textField
        commandInput = GUI.TextField(GUIHelper.RatioRect(0.18f, 0.85f, 0.65f, DebugConsole.BTN_R_HEIGHT), commandInput);

        // Focus textField
        if (GUI.GetNameOfFocusedControl() == string.Empty) GUI.FocusControl(controlName);

        // Draw run button
        if (GUI.Button(GUIHelper.RatioRect(0.84f, 0.85f, DebugConsole.BTN_R_WIDTH, DebugConsole.BTN_R_HEIGHT), "Run")
            || (Event.current.type == EventType.Used && Event.current.keyCode == KeyCode.Return))
        {
            if(!string.IsNullOrEmpty(commandInput))
            {
                ParseConsoleCommandStr(commandInput);
                commandInput = "";
            }
        }
    }

    public void AddConsoleCommand(string id, Action<List<string>> callback)
    {
        if (commandDic.ContainsKey(id))
            throw new Exception("Has exist command");
        else
            commandDic.Add(id, new ConsoleConmand() { id = id, callback = callback });
    }

    public void AddConsoleCommand(ConsoleConmand command)
    {
        if (commandDic.ContainsKey(command.id))
            throw new Exception("Has exist command: " + command.id);
        else
            commandDic.Add(command.id, command);
    }

    public void ParseConsoleCommandStr(string input)
    {
        string[] cmdPeer = input.Split(' ');
        if(cmdPeer.Length != 0)
        {
            string commandName = cmdPeer[0];

            // Find command
            if(commandDic.ContainsKey(commandName))
            {
                if(cmdPeer.Length > 1) 
                {
                    // Invoke callback with arguments
                    List<string> args = new List<string>(cmdPeer);
                    args.RemoveAt(0);

                    commandDic[commandName].callback(args);
                }
                else 
                {
                    // Or invoke callback with null
                    commandDic[commandName].callback(null);
                }
            }
            else
            {
                Watchdog.Log(Name, "Command '" + commandName + "' not found");
            }
        }
    }

    public T GetCommand<T>(string id) where T : ConsoleConmand
    {
        if (!commandDic.ContainsKey(id))
        {
            Watchdog.LogError(Name, "GetCommand '" + id + "' not found");
            return null;
        }
        else
        {
            return commandDic[id] as T;
        }
    }

}
